<?php

namespace App\Http\Controllers\Admin;

use App\Model\BasePicture;
use App\Model\Secify;
use App\Model\Spot;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SpotController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keywords = $request->input('keywords');

        $m = Spot::where('state',1)->with('onePicture');
        if($keywords) {
            $m = $m->where([['name','like','%'.$keywords.'%']]);
        }else{
            $keywords = '';
        }

        $data = $m->orderBy('id','desc')->paginate(15);
        $secify = Secify::get();

        return view('admin.place.place-spot-list',compact('data','secify','keywords'));
//        foreach ($data as $key => $value){
//            $items[] = BasePicture::where('spot_id',$value->id)->first();
//        }

//        return view('admin.place.place-spot-list',compact('data','secify','items','page'));

//        $page = Spot::where('state',1)->paginate(15);
//        $data = Spot::where('state',1)->orderBy('id','desc')->get();
//        foreach ($data as $key => $value){
//            $items[] = BasePicture::where('spot_id',$value->id)->first();
//        }
//
//        $secify = Secify::get();
//        return view('admin.place.place-spot-list',compact('data','secify','items','page'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $m = Spot::where('id',$id)->first();
        $item = $m->basePicture()->get();
        return view('admin.place.place-spot-picture-show',compact('item'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item = Spot::where('id',$id)->first();
        $secify = Secify::get();
        $data = $item->baseReply()->with('user')->get();
//        dd($data);
        return view('admin.place.place-spot-show',compact('item','secify','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Spot::where('id',$id)->first()->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    public function search(Request $request)
    {
        $keyword = $request->input('keywords');
        if ($keyword != null){
            $page = Spot::where([['name','like','%'.$keyword.'%'],['state',1]])->paginate(15);

            $data = Spot::where([['name','like','%'.$keyword.'%']])->get();
            foreach ($data as $key => $value){
                $items[] = BasePicture::where('spot_id',$value->id)->first();
            }
            if ($page){
                $secify = Secify::get();
                return view('admin.place.place-spot-search',compact('data','secify','keyword','page','items'));
            }
            return view('admin.place.place-spot-search');
        }else {
            return view('admin.place.place-spot-search',compact('keyword'));
        }
    }
}
